#include <cassert>
#include <cstring>
#include <iostream>
#include <cstdlib>   
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <termio.h>
#include <fcntl.h>
#include <getopt.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>

#define MAX_LEN 200
char serialrate[MAX_LEN];
char serialport[MAX_LEN];
char serialbit[MAX_LEN];
char serialparity[MAX_LEN];
char serialstop[MAX_LEN];

using namespace std;

void readuci(char cmd[], char para[]) //从uci命令中获取参数
{
	FILE *fp;
	int i = 0;
	if((fp = popen(cmd,"r")) == NULL)
	{
		printf("Get serviceid false!,cmd=%s,para=%s\n",cmd,para);
		sprintf(para,"%s","00000000");
		return;
	}
	memset(para,0,sizeof(para));
	if(fgets(para,MAX_LEN, fp)!=NULL)
	{
		while(para[i] != '\n')
		{
			i++;
		}
		para[i] = '\0';
	}
	pclose(fp);
}


int main()
{
	sleep(3);
	readuci("uci get routedif.@routedif[0].serialport",serialport);
	readuci("uci get routedif.@routedif[0].serialrate",serialrate);
	readuci("uci get routedif.@routedif[0].serialbit",serialbit);
	readuci("uci get routedif.@routedif[0].serialparity",serialparity);
	readuci("uci get routedif.@routedif[0].serialstop",serialstop);

	system("killall ser2net");
	sleep(1);
	char cmd[400];
	if(strcmp(serialstop,"1")==0)
		sprintf(cmd,"echo %s:raw:600:/dev/ttyS1:%s %s %sSTOPBIT %sDATABITS >/etc/ser2net.conf",serialport,serialrate,serialparity,serialstop,serialbit);
	else
		sprintf(cmd,"echo %s:raw:600:/dev/ttyS1:%s %s %sSTOPBITS %sDATABITS >/etc/ser2net.conf",serialport,serialrate,serialparity,serialstop,serialbit);
	system(cmd);
	sleep(1);
	system("ser2net");
	
}

